// 全局共享数据示例
import { DEFAULT_NAME } from '@/constants';
import { useState,useCallback } from 'react';

const useUser = () => {
  const [name, setName] = useState<string>(DEFAULT_NAME);
  const [allToken, setAllToken] = useState<string>(localStorage.getItem('tc_all_token')||''); // 登录token
  const [currentAppMenu, setCurrentAppMenu] = useState<any>(); // 当前选择的子项目
  const [currentSelectTab, setCurrentSelectTab2] = useState<any>(); // 当前tab选择的内容
  const [currentTabList, setCurrentTabList] = useState<any>( localStorage.getItem('currentTabList') as any ? JSON.parse(localStorage.getItem('currentTabList') as any) || []:[] );
  /* 修改当前tab选择的内容 */
  const setCurrentSelectTab  = (_new:any) => {
    setCurrentSelectTab2(_new);
    localStorage.setItem('currentSelectTab',JSON.stringify(_new));
  };

  /* 修改当前tab list */
  const updatedCurrentTabList  = (_newlist:any) => {
    setCurrentTabList(_newlist);
    localStorage.setItem('currentTabList',JSON.stringify(_newlist));
  };

  /* 按钮：关闭除首页外的其它页面 */
  const removeCurrentTabList  = () => {
    setCurrentTabList([ currentTabList[0]]);
    localStorage.setItem('currentTabList',JSON.stringify(currentTabList[0]));
  };

  /*  清楚所有tab */
  const clearCurrentTabList  = () => {
    setCurrentTabList([]);
    localStorage.removeItem('currentTabList');
    setCurrentSelectTab({});
  };

  
  /*  清楚所有Global数据 */
  const clearAllGlobal  = () => {
    setCurrentTabList([]);
    // localStorage.removeItem('currentTabList');
    setCurrentSelectTab({});
    setCurrentAppMenu({});
    setName('');
    setAllToken('');
    // localStorage.removeItem('tc_all_token');
    localStorage.clear();
  };

  /*  登录token */
  const setLoginToken  = (_s:string) => {
    setAllToken(_s);
    localStorage.setItem('tc_all_token',_s);
  };

  return {
    name,
    setName,
    currentAppMenu,
    setCurrentAppMenu,
    currentSelectTab,
    setCurrentSelectTab,
    currentTabList,
    setCurrentTabList,
    updatedCurrentTabList,
    removeCurrentTabList,
    clearCurrentTabList,
    clearAllGlobal,
    allToken,
    setLoginToken
  };
};

export default useUser;
